const {
	Service
} = require('uni-cloud-router')

module.exports = class IntegralService extends Service {

	constructor(ctx) {
		super(ctx)
	}

	/**
	 * 增加积分
	 */
	async increase(uid, uname, integralObj, dbtran) {
		const integral = integralObj.integral;

		// 不存在积分，则返回
		if (integral <= 0) return;
		
		const utc8date = this.service.utils.toDateUTC8(Date.now());
		const time = this.service.utils.formatdate(utc8date, 'yyyy-MM-dd hh:mm');
		
		let updateObj = {
			member_integral: this.db.command.inc(integral),
			member_integral_total: this.db.command.inc(integral),
			member_integral_surplus: this.db.command.inc(integral)
		};
		if (integralObj.source == '签到') {
			updateObj.member_integral_sign_cnt = this.db.command.inc(1)
		}
		
		// 存在则使用事务模式
		dbtran = dbtran || this.db;
		
		// 00. 会员积分记录
		await dbtran.collection('wmzmall-member').doc(uid).update(updateObj);

		// 01. 记录获取积分记录
		await dbtran.collection('wmzmall-integral-record').add({
			member_id: uid,
			member_uid: uid,
			integral: integral,
			type: '收入',
			source: integralObj.source,
			order_id: integralObj.order_id,
			goods_id: integralObj.goods_id,
			remark: `+${integral} 积分`,
			state: '正常',
			is_delete: 0,
			time: time,
			create_time: Date.now(),
			create_uid: uid,
			create_uname: uname
		});
		
		// 02. 积分日志
		await dbtran.collection('wmzmall-integral-log').add({
			member_id: uid,
			member_uid: uid,
			integral: integral,
			ip: this.ctx.context.CLIENTIP,
			type: integralObj.source,
			content: `+${integral} 积分`,
			remark: `${integralObj.source} +${integral} 积分`,
			state: '正常',
			is_delete: 0,
			time: time,
			create_time: Date.now(),
			create_uid: uid,
			create_uname: uname
		});
	}

	/**
	 * 减少积分
	 */
	async decrease(uid, uname, integralObj, dbtran) {
		const integral = integralObj.integral;
		
		// 不存在积分，则返回
		if (integral <= 0) return;
		
		const utc8date = this.service.utils.toDateUTC8(Date.now());
		const time = this.service.utils.formatdate(utc8date, 'yyyy-MM-dd hh:mm');
		
		let updateObj = {
			member_integral: this.db.command.inc(-integral),
			member_integral_surplus: this.db.command.inc(-integral)
		};
		
		// 存在则使用事务模式
		dbtran = dbtran || this.db;
		
		// 00. 会员积分记录
		await dbtran.collection('wmzmall-member').doc(uid).update(updateObj);
		
		// 01. 记录获取积分记录
		await dbtran.collection('wmzmall-integral-record').add({
			member_id: uid,
			member_uid: uid,
			integral: integral,
			type: '支出',
			source: integralObj.source,
			order_id: integralObj.order_id,
			goods_id: integralObj.goods_id,
			remark: `-${integral} 积分`,
			state: '正常',
			is_delete: 0,
			time: time,
			create_time: Date.now(),
			create_uid: uid,
			create_uname: uname
		});
		
		// 02. 积分日志
		await dbtran.collection('wmzmall-integral-log').add({
			member_id: uid,
			member_uid: uid,
			integral: integral,
			ip: this.ctx.context.CLIENTIP,
			type: integralObj.source,
			content: `-${integral} 积分`,
			remark: `${integralObj.source} -${integral} 积分`,
			state: '正常',
			is_delete: 0,
			time: time,
			create_time: Date.now(),
			create_uid: uid,
			create_uname: uname
		});
	}
}
